<?php
		
	$_options 	= array();
	$_type 		= $_REQUEST['type'];
	$_value 	= explode(",", $_REQUEST['value']);
	
	switch($_REQUEST['type']){
		default:{
			$filename = dirname(__FILE__)."/".$_type.".txt";
			//first, obtain the data initially present in the text file
			$ini_handle = fopen($filename, "r+");
			$ini_contents = fread($ini_handle, filesize($filename));
			
			if($ini_contents == "none" || isset($_REQUEST['reload'])){
				$options = array();
				$options[] = "<option value=''>[None]</option>";
				$options = array_merge(  $options, getFilesAndFolder("Template", "/templates/"));
				$options = array_merge(  $options, getFilesAndFolder("Components", "/components/"));
				$options = array_merge(  $options, getFilesAndFolder("Module", "/modules/"));
				$options = array_merge(  $options, getFilesAndFolder("Plugin", "/plugins/"));
				
				$ini_handle = fopen($filename, "w+");
				$writestring = implode("\n", $options);
				if (fwrite($ini_handle, $writestring) === false) {
					echo "Cannot write to text file. <br />";          
				}
				$ini_contents = $writestring;
			}
			$options = explode("\n", $ini_contents);
			fclose($ini_handle);
			
			$str = '<select size="15" id="'.$_REQUEST['name'].'" name="'.$_REQUEST['control_name'].'['.$_REQUEST['name'].'][]" style="height:200px" cols="50" multiple="multiple" class="inputbox">';
			foreach ($options as $opt){
				preg_match("/value=\'(.*)\'/", $opt, $value);
				if(in_array($value[1], $_value))
					$str .= str_replace("<option ", "<option selected='selected' ", $opt);
				else
					$str .= $opt;
			}
			$str .= "</select>";
			echo $str;
			break;
		}
	}
	
	function getFilesAndFolder($name, $root){
		global $_options;
		$__options = array ();
		$__options[] = "<option value='{$root}' style='font-weight:bold'>{$name}</option>";
		$_options = array();
		recusiveFolder($_REQUEST['path'].$root, true);
		$__options = array_merge($__options, $_options);
		$_options = array();
		return $__options;
	}
	
	function recusiveFolder($folder, $first=false){
		global $_options;
		if($handle = opendir($folder)){
			while($file = readdir($handle)){
				clearstatcache();
				if(is_dir($folder.'/'.$file) && $file != "." && $file != ".."){
					if($first)
						$_options[] = "<option value='".replaceValue($folder.$file)."'>".printSpace(3).$file."</option>";
					$_options = array_merge($_options, searchFilesInFolder($folder.$file."/"));
					
					recusiveFolder($folder.$file."/", false);
				}
			}
			closedir($handle);
		}
	}
	
	function searchFilesInFolder($folder){
		global $_type;		
		$__options = array ();		
		if($handle = opendir($folder)){
			while($file = readdir($handle)){
				clearstatcache();				
				if(is_file($folder.$file)){					
					$name = pathinfo($folder.$file);
					$name = (isset($name['extension'])?$name['extension']:"");
					$size = calculateSize(filesize($folder.$file));
					if($name == $_type)
						$__options[] = "<option value='".replaceValue($folder.$file)."'>".printSpace(9).$file." [{$size}]</option>";
				}
			}
			closedir($handle);
		}		
		return $__options;		
	}
	
	function replaceValue($string){
		$string = str_replace($_REQUEST['path'], "", $string);
		$string = str_replace(DIRECTORY_SEPARATOR , "/", $string);
		return $string;
	}
	
	function calculateSize($size, $sep = ' '){
		$unit = null;
		$units = array('B', 'KB', 'MB', 'GB', 'TB');	
		for($i = 0, $c = count($units); $i < $c; $i++){
			if ($size > 1024){
				$size = $size / 1024;
			}else{
				$unit = $units[$i];
				break;
			}
		}	
		return round($size, 2).$sep.$unit;
	}
	
	function printSpace($num){
		$str = "";
		for($i=0; $i<$num; $i++){
			$str .= "&nbsp;";
		}
		return $str;
	}
	
?>